package com.xjcs.saplingplan.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RadialGradient
import android.graphics.Shader
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.View
import kotlin.random.Random

class PersonalityBubbleView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    // 气泡数据模型
    data class Bubble(
        val text: String,
        val color: Long,
        var x: Float,
        var y: Float,
        var radius: Float,
        var speedX: Float,
        var speedY: Float
    )

    private val bubbles = mutableListOf<Bubble>().apply {
        // 根据图片描述初始化气泡（颜色、大小、文字）
        add(Bubble("爱跳舞", 0xFFFF6B6B, 0f, 0f, 80f, 0.5f, 0.5f))
        add(Bubble("有趣", 0xFF4ECDC4, 0f, 0f, 70f, -0.3f, 0.6f))
        add(Bubble("有生活情调", 0xFF45B7D1, 0f, 0f, 90f, 0.4f, -0.2f))
        add(Bubble("有幽默感", 0xFFA593E0, 0f, 0f, 75f, -0.5f, -0.4f))
        add(Bubble("擅长摄影", 0xFFFFC154, 0f, 0f, 85f, 0.3f, 0.3f))
        add(Bubble("厨艺好", 0xFF6BD2A0, 0f, 0f, 65f, -0.6f, 0.2f))
        add(Bubble("善良", 0xFFF78FB3, 0f, 0f, 60f, 0.2f, -0.5f))
        add(Bubble("温柔细心", 0xFF9B59B6, 0f, 0f, 70f, -0.2f, 0.7f))
        add(Bubble("浪漫", 0xFF3498DB, 0f, 0f, 55f, 0.7f, -0.3f))
    }

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.FILL
    }

    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.WHITE
        textSize = 24f
        textAlign = Paint.Align.CENTER
        typeface = Typeface.DEFAULT_BOLD
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        // 初始随机位置（避开边缘）
        bubbles.forEach { bubble ->
            bubble.x = (bubble.radius + Random.nextFloat() * (w - 2 * bubble.radius))
            bubble.y = (bubble.radius + Random.nextFloat() * (h - 2 * bubble.radius))
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        
        // 1. 绘制黑色背景
        canvas.drawColor(Color.BLACK)
        
        // 2. 绘制黄色边框（简化版）
        val borderPaint = Paint().apply {
            color = Color.YELLOW
            style = Paint.Style.STROKE
            strokeWidth = 8f
        }
        canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), borderPaint)
        
        // 3. 绘制气泡（三层效果）
        bubbles.forEach { bubble ->
            // 内层核心（不透明）
            paint.shader = RadialGradient(
                bubble.x, bubble.y, bubble.radius * 0.3f,
                Color.argb(255, Color.red(bubble.color.toInt()), Color.green(bubble.color.toInt()), Color.blue(bubble.color.toInt())),
                Color.argb(200, Color.red(bubble.color.toInt()), Color.green(bubble.color.toInt()), Color.blue(bubble.color.toInt())),
                Shader.TileMode.CLAMP
            )
            canvas.drawCircle(bubble.x, bubble.y, bubble.radius, paint)
            
            // 中层渐变（缓慢过渡）
            paint.shader = RadialGradient(
                bubble.x, bubble.y, bubble.radius * 0.75f,
                Color.argb(200, Color.red(bubble.color.toInt()), Color.green(bubble.color.toInt()), Color.blue(bubble.color.toInt())),
                Color.argb(80, Color.red(bubble.color.toInt()), Color.green(bubble.color.toInt()), Color.blue(bubble.color.toInt())),
                Shader.TileMode.CLAMP
            )
            canvas.drawCircle(bubble.x, bubble.y, bubble.radius, paint)
            
            // 外层边缘（快速透明）
            paint.shader = RadialGradient(
                bubble.x, bubble.y, bubble.radius,
                Color.argb(80, Color.red(bubble.color.toInt()), Color.green(bubble.color.toInt()), Color.blue(bubble.color.toInt())),
                Color.TRANSPARENT,
                Shader.TileMode.CLAMP
            )
            canvas.drawCircle(bubble.x, bubble.y, bubble.radius, paint)
            
            paint.shader = null
            
            // 白色描边（增强边缘）
            paint.style = Paint.Style.STROKE
            paint.color = Color.argb(100, 255, 255, 255)
            paint.strokeWidth = 1.5f
            canvas.drawCircle(bubble.x, bubble.y, bubble.radius, paint)
            paint.style = Paint.Style.FILL
            
            // 调整文字大小根据气泡尺寸
            textPaint.textSize = bubble.radius * 0.3f
            // 绘制文字（居中）
            canvas.drawText(bubble.text, bubble.x, bubble.y + textPaint.textSize/3, textPaint)
        }
        
        // 4. 绘制标题文字
        val titlePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            color = Color.WHITE
            textSize = 36f
            textAlign = Paint.Align.CENTER
            typeface = Typeface.DEFAULT_BOLD
        }
        canvas.drawText("点亮更多特征", width/2f, 100f, titlePaint)
        canvas.drawText("精准推荐你喜欢的类型", width/2f, 150f, titlePaint.apply { textSize = 28f })
        
        // 更新气泡位置（模拟浮动效果）
        updateBubbles()
        invalidate()
    }

    private fun updateBubbles() {
        bubbles.forEach { bubble ->
            bubble.x += bubble.speedX
            bubble.y += bubble.speedY
            
            // 边界检测
            if (bubble.x < bubble.radius || bubble.x > width - bubble.radius) {
                bubble.speedX *= -1
            }
            if (bubble.y < bubble.radius || bubble.y > height - bubble.radius) {
                bubble.speedY *= -1
            }
        }
    }
}